/**
 * Created by a1 on 16/9/23.
 */

function PageListData() {

    this.Size = 10
    this.timeStamp = 0
    this.isLoading = false

    this.arrayData = new Array()
    this.nodeListContent
    this.nodeList
}

PageListData.prototype.initNode = function() {
    this.nodeListContent = $("#dm_content_list")
    this.nodeList = $("#dm_list")
}

var pageListData
function initList() {
    pageListData = new PageListData()
    $("#dm_title_list").html("列表")
}

function onShowList() {
    pageListData.initNode()
    bindRefresh(pageListData.nodeListContent, function() {
        onRefresh()
    })
    if(pageListData.arrayData.length == 0) {
        $.pullToRefreshTrigger(pageListData.nodeListContent)
    }else {
        bindListLoadMore()
    }
}

function bindListLoadMore() {
    addLoadMore(pageListData.nodeListContent)
    bindLoadMore($("#dm_page_list"), function() {
        onLoadMore()
    })
}

function onRefresh() {
    pageListData.timeStamp = 0
    apiZaTanList()
}

function onLoadMore() {
    apiZaTanList()
}

function apiZaTanList() {
    if(pageListData.isLoading) {
        return
    }
    pageListData.isLoading = true
    var url = "../test/data"
    $.getJSON(url, function(data){
        pageListData.isLoading = false
        $.pullToRefreshDone(pageListData.nodeListContent)
        var length = data.length
        //console.log("data = " + length)
        if(length > 0) {
            if(pageListData.timeStamp == 0) {
                pageListData.arrayData.length = 0
                pageListData.nodeList.html("")
                $.refreshScroller()
                if(length >= pageListData.Size) {
                    console.log("nodeListContent =" + pageListData.nodeListContent)
                    bindListLoadMore()
                }
            }
            pageListData.timeStamp = 1
        }
        if(length < pageListData.Size) {
            removeLoadMore(pageListData.nodeListContent)
        }
        pageListData.arrayData = pageListData.arrayData.concat(data)
        for(var i = 0; i < length; ++i) {
            var itemData = data[i]
            addListItem(pageListData.nodeList, itemData, "3")
        }
        $.refreshScroller()
    })
}

//function addItemZatan(parent, index, data) {
//    //console.log(index + " = " + JSON.stringify(data))
//    var item = $("<div></div>")
//    var content = $("<div class='dm_list_item' container='middle'></div>")
//    var containerLeft = $("<div class='dm_list_item_container_left' container='middle'></div>")
//    var labelTitle = $("<div class='dm_list_item_text_title'>"+data[kTitle]+"</div>")
//    var labelContent = $("<div class='dm_list_item_text_content'  container='middle'>"+data[kContent]+"</div>")
//    var labelDate = $("<div class='dm_list_item_text_date'>"+data[kDate]+"</div>")
//    containerLeft.append(labelTitle)
//    containerLeft.append(labelContent)
//    containerLeft.append(labelDate)
//    content.append(containerLeft)
//
//    var imageUrl = data[kImageUrl]
//    var hasImage = ($.trim(imageUrl) != "")
//    if(hasImage) {
//        var imageView = $("<img class='dm_list_item_image' container='middle' src='"+imageUrl+"'/>")
//        content.append(imageView)
//        //重置文本与item的size
//        containerLeft.addClass("dm_shrink")
//        content.addClass("dm_shrink")
//    }
//    var divider = "<div class='dm_divider'></div>"
//    item.append(content)
//    item.append(divider)
//    parent.append(item)
//
//    item.click(function() {
//        //toDetail(data)
//    })
//}